package com.kongzhong;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

public class QueueController extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("in doPost...");
		String type = req.getParameter("type");
		System.out.println("[type][" + type + "]");
		if ("getQueueSize".equals(type)) {
			getQueueSize(req, resp);
		} else if ("offerObj".equals(type)) {
			offerObj(req, resp);
		} else if ("pollObj".equals(type)) {
			pollObj(req, resp);
		} else if ("clearQueue".equals(type)) {
			clearQueue(req, resp);
		}
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	public static void writeStringIntoResponse(HttpServletResponse response, String string) {
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.print(string);
			out.flush();
		} catch (Exception e) {
			out.write("error");
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}

	public void getQueueSize(HttpServletRequest request, HttpServletResponse response) {
		String jsonString = request.getParameter("jsonpcallback");
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", true);
		try {
			jsonObject.put("info", Queue.getQueueSize());
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put("result", false);
		}
		writeStringIntoResponse(response, jsonString + "(" + jsonObject + ")");
	}

	public void offerObj(HttpServletRequest request, HttpServletResponse response) {
		String jsonString = request.getParameter("jsonpcallback");
		JSONObject jsonObject = new JSONObject();
		String str = UUID.randomUUID().toString();
		boolean flag = Queue.offerObj(str);
		int i = 0;
		while (!flag && i < 5) {
			try {
				flag = Queue.offerObj(str);
				i++;
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		jsonObject.put("result", flag);
		writeStringIntoResponse(response, jsonString + "(" + jsonObject + ")");
	}

	public void pollObj(HttpServletRequest request, HttpServletResponse response) {
		String jsonString = request.getParameter("jsonpcallback");
		JSONObject jsonObject = new JSONObject();
		Object obj = Queue.pollObj();
		int i = 0;
		while (obj == null && i < 5) {
			try {
				obj = Queue.pollObj();
				i++;
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		if (obj == null) {
			jsonObject.put("result", false);
		} else {
			jsonObject.put("result", true);
		}
		jsonObject.put("info", obj + "");
		writeStringIntoResponse(response, jsonString + "(" + jsonObject + ")");
	}

	public void clearQueue(HttpServletRequest request, HttpServletResponse response) {
		String jsonString = request.getParameter("jsonpcallback");
		JSONObject jsonObject = new JSONObject();
		String sign = request.getParameter("sign");
		boolean flag = Queue.clearQueue(sign);
		jsonObject.put("result", flag);
		writeStringIntoResponse(response, jsonString + "(" + jsonObject + ")");
	}

}
